package com.dc.commons.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

import com.google.common.collect.Lists;

/*******************************************************************************<P>
 * 文件名: FtpUtil.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年6月13日 下午3:36:08<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
public class FtpUtil {

	public static class FTP{
		private FTPClient ftpClient;
		
		private String host;
		private int port;
		private int dataTimeout=30000;
		private int connectTimeout=30000;
		
		public void connect()throws Exception{
			
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
			conf.setServerLanguageCode("zh");
			ftpClient.configure(conf);
			Charset charset = Charset.forName("UTF-8");
			ftpClient.setCharset(charset);
			String encoding="GBK";
			ftpClient.setControlEncoding(encoding);
			ftpClient.setConnectTimeout(connectTimeout);
			ftpClient.setDataTimeout(dataTimeout);
			ftpClient.connect(host, port);
		}
		
		public boolean login(String username,String password){
			boolean flag=true;
			try{
				this.connect();
				ftpClient.login(username, password);
				ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
				ftpClient.enterLocalPassiveMode(); 
				ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			int reply = ftpClient.getReplyCode();    
            if (!FTPReply.isPositiveCompletion(reply)) {    
            	try {
					ftpClient.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}    
            	flag=false;    
            }    
			
			return flag;
		}
		
		public boolean logout(){
			boolean flag=false;
			try {
				flag= ftpClient.logout();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					ftpClient.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return flag;
		}
		
		
		public void upload(String dir,String fileName,InputStream input)throws IOException{
			changeWorkingDirectory(dir);
			ftpClient.storeFile(fileName, input);
			ftpClient.cwd("/");
			input.close();
		}
		
		public void upload(String dir,String fileName,File file)throws IOException{
			upload(dir, fileName, new FileInputStream(file));
		}
		
		public void downLoad(String dir,String fileName,OutputStream out)throws IOException{
			changeWorkingDirectory(dir);
			if(ftpClient.listNames(fileName).length>0){
				ftpClient.retrieveFile(fileName, out);
				out.close();
			}else{
				throw new RuntimeException(dir+"/"+fileName+" 不存在！");
			}
			ftpClient.cwd("/");
		}
		
		
		
		
		public void changeWorkingDirectory(String dirs) throws IOException{
			for(String dir:dirs.split("/")){
				List<String> names=Lists.newArrayList(ftpClient.listNames());
				if(!names.contains(dir)){
					ftpClient.makeDirectory(dir);
				}
				ftpClient.changeWorkingDirectory(dir);
			}
		}
		
	}
	
	public static boolean upload(String base,String name,File file){
		return false;
	}
}
